<?php
/**
 * User: Hufeng
 * Date: 2017/12/04 17:51
 * Desc: TRADE 图片上传服务
 */
namespace App\Controller;
use App\Common\Util\AliOss;
use App\Common\Util\Image;

class UploadImg {
    /*上传图片*/
    public function index(){
        $res = [];
        if($_FILES){
            $AliOss = new AliOss();
            foreach ($_FILES as $val){
                $date = date('Ymd');
                $time = time();
                $fileDir = 'YltmallRes/'.$date.'/';
                $fileExt = pathinfo($val['name'], PATHINFO_EXTENSION);
                $fileName = $time.rand(11111,99999).'.'.$fileExt;
                if(!file_exists(STATIC_FILE_DIR.$fileDir)){
                    mkdir(STATIC_FILE_DIR.$fileDir);
                }
                move_uploaded_file($val['tmp_name'],STATIC_FILE_DIR.$fileDir.$fileName);
                $con = file_get_contents(STATIC_FILE_DIR.$fileDir.$fileName);
                $AliOss->putObject($fileDir.$fileName,$con);
                $res[] = ALI_OSS_URL.'/'.$fileDir.$fileName;
            }
            return json_encode(['code'=>0,'msg'=>'上传成功','data'=>$res]);
        }
        return json_encode(['code'=>1,'msg'=>'上传失败']);
    }

    /*编辑器上传图片*/
    public function wangedit(){
        $res = [];
        $res['errno'] = 0;
        if($_FILES){
            $AliOss = new AliOss();
            $Image = new Image();
            foreach ($_FILES as $val){
                $date = date('Ymd');
                $time = time();
                $fileDir = 'YltmallRes/'.$date.'/';
                $fileExt = pathinfo($val['name'], PATHINFO_EXTENSION);
                $fileName = $time.rand(11111,99999).'.'.$fileExt;
                if(!file_exists(STATIC_FILE_DIR.$fileDir)){
                    mkdir(STATIC_FILE_DIR.$fileDir);
                }
                move_uploaded_file($val['tmp_name'],STATIC_FILE_DIR.$fileDir.$fileName);
                $Image->compress(STATIC_FILE_DIR.$fileDir.$fileName);

                $con = file_get_contents(STATIC_FILE_DIR.$fileDir.$fileName);
                $AliOss->putObject($fileDir.$fileName,$con);
                $res['data'][] = ALI_OSS_URL.'/'.$fileDir.$fileName;
            }
        }
        return json_encode($res);
    }
    /*裁切图片*/
    public function cropImg(){
        $imgFile = $_POST['imgDir'];
        $w = $_POST['cwidth'];
        $h = $_POST['cheight'];
        $x = $_POST['cleft'];
        $y = $_POST['ctop'];
        $fw = $_POST['fixWidth'];
        $Image = new Image();
        $fileDir = $Image->cropImg($imgFile,$w,$h,$x,$y,$fw);
        $AliOss = new AliOss();

        $con = file_get_contents(STATIC_FILE_DIR.$fileDir);
        $AliOss->putObject(ltrim($fileDir,'/'),$con);
        $res['code'] = 0;
        $res['file'] = $fileDir;
        return json_encode($res);
    }

    /*阿里身份证识别*/
    public function idCardCheck(){
        if($_FILES){
            $AliOss = new AliOss();
            foreach ($_FILES as $val){
                $date = date('Ymd');
                $time = time();
                $fileDir = 'YltmallRes/'.$date.'/';
                $fileExt = pathinfo($val['name'], PATHINFO_EXTENSION);
                $fileName = $time.rand(11111,99999).'.'.$fileExt;
                if(!file_exists(STATIC_FILE_DIR.$fileDir)){
                    mkdir(STATIC_FILE_DIR.$fileDir);
                }
                move_uploaded_file($val['tmp_name'],STATIC_FILE_DIR.$fileDir.$fileName);
                $con = file_get_contents(STATIC_FILE_DIR.$fileDir.$fileName);
                $AliOss->putObject($fileDir.$fileName,$con);
                $imgCode = file_get_contents(STATIC_FILE_DIR.$fileDir.$fileName);
                $checkData = $this->aliCard(base64_encode($imgCode));
                if($checkData['success']){
                    $checkData['imgurl'] = ALI_OSS_URL.'/'.$fileDir.$fileName;
                    return json_encode(['code'=>0,'msg'=>'认证成功','data'=>$checkData]);
                }
            }
        }
        return json_encode(['code'=>1,'msg'=>'身份证照片不清晰，请重新上传']);
    }
    public function aliCard($imgCode){
        $host = "https://dm-51.data.aliyun.com";
        $path = "/rest/160601/ocr/ocr_idcard.json";
        $method = "POST";
        $appcode = "c225646f54c045adbf4f7afd6d4c1b3c";
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
        $bodys = "{
    \"inputs\": [
        {
            \"image\": {
                \"dataType\": 50,
                \"dataValue\": \"{$imgCode}\"    
            },
            \"configure\": {
                \"dataType\": 50,
                \"dataValue\": \"{\\\"side\\\":\\\"face\\\"}\"
            }
        }
    ]
}";
        $url = $host . $path;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$".$host, "https://")){
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $res = curl_exec($curl);
        $start = stripos($res,'dataValue":"')+12;
        $jsonRes = substr($res,$start,-6);
        $jsonRes = str_replace('\\','',$jsonRes);
        $jsonRes = str_replace('"config_str":"{"side":"face"}",','',$jsonRes);
        $jsonRes = str_replace('"{','{',$jsonRes);
        $jsonRes = str_replace('}"','}',$jsonRes);
        $data = json_decode($jsonRes,true);
        return $data;
    }
}
